<template>
  <div class="wrap">
    <app-title title="创建新应用"></app-title>
    <div class="content-wrapper">
      <h3>基础信息</h3>
      <el-form ref="form" :rules="rules" :model="appInfo" label-width="80px" label-position="left">
        <el-form-item label="默认语言" prop="defaultLang">
          <el-select v-model="appInfo.defaultLang" placeholder="请选择默认语言">
            <el-option label="简体中文" value="简体中文"></el-option>
            <el-option label="英文" value="英文"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="操作系统" prop="operatingSystem">
          <el-radio class="radio" v-model="appInfo.operatingSystem" label="android">Android</el-radio>
          <el-radio class="radio" v-model="appInfo.operatingSystem" label="ios">IOS</el-radio>
        </el-form-item>
        <el-form-item label="应用名称" prop="name">
          <el-input v-model="appInfo.name" placeholder="请输入应用名称"></el-input>
          <p class="tip">请输入您的应用名称,不超过8个汉字或16个字符,以后可以修改</p>
        </el-form-item>
        <el-form-item label="应用包名" prop="packageName">
          <el-input v-model="appInfo.packageName" placeholder="请输入内容"></el-input>
          <p class="tip">请注意包名一旦填写不能修改,请填写一个足够复杂,不易重复的包名,以免影响发布</p>
        </el-form-item>
      </el-form>

      <el-button type="primary" class="create-button" @click="handleCreate">
        创建
      </el-button>
    </div>
  </div>
</template>

<script>
  import AppTitle from '../AppTitle'
  import { createNewApp } from '@/api/apps'

  export default {
    components: {
      'app-title': AppTitle
    },
    data(){
      return{
        appInfo:{
          name: '',
          defaultLang: '简体中文',
          operatingSystem: 'android',
          packageName: ''
        },
        options: [
          {
            value: '简体中文',
            label: '简体中文'
          },
          {
            value: '英文',
            label: '英文'
          }
        ],
        rules: {
          defaultLang: [
            { required: true, message: '请输入活动名称', trigger: 'blur' },
          ],
          operatingSystem: [
            { required: true, trigger: 'blur' },
          ],
          name: [
            { required: true, message: '请输入应用名称', trigger: 'blur' },
          ],
          packageName: [
            { required: true, message: '请输入应用包名', trigger: 'blur' },
          ]
        }
      }
    },
    methods: {
      handleCreate(){
        this.$refs.form.validate((valid) => {
          if (valid) {
            createNewApp(this.appInfo).then( res => {
              this.$router.push({name: 'entry-list'})
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      }
    }
  }
</script>

<style lang="scss" scoped>
  .content-wrapper{
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    margin-left: 20px;
    .el-form-item{
      margin-bottom: 40px;
    }
    .tip{
      color: #a6a6a6;
      margin: 0;
    }
    .create-button{
      margin-left: 80px;
    }
  }
</style>
